package com.yedean.mall.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yedean.mall.api.CommonPage;
import com.yedean.mall.api.CommonResult;
import com.yedean.mall.entity.UmsRole;
import com.yedean.mall.service.IUmsRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 后台用户角色管理
 *
 * @author yedean
 */
@Api(tags = "后台用户角色管理")
@RequestMapping("/admin/role")
@RestController
public class UmsRoleController {

    @Autowired
    private IUmsRoleService roleService;


    @ApiOperation("添加角色")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public CommonResult<?> create(@RequestBody UmsRole role) {

        boolean save = roleService.save(role);
        if (save) {
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

    @ApiOperation("修改角色")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
    public CommonResult<?> update(@PathVariable Long id, @RequestBody UmsRole role) {
        role.setId(id);
        boolean update = roleService.updateById(role);
        if (update) {
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

    @ApiOperation("批量删除角色")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public CommonResult<?> delete(@RequestParam("ids") List<Long> ids) {
        boolean remove = roleService.removeBatchByIds(ids);
        if (remove) {
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

    @ApiOperation("根据角色名称分页获取角色列表")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public CommonResult<CommonPage<UmsRole>> list(@RequestParam(value = "keyword", required = false) String keyword,
                                                  @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                                  @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {


        IPage<UmsRole> list = roleService.list(keyword, pageSize, pageNum);
        return CommonResult.success(CommonPage.resetPage(list));
    }

    @ApiOperation("修改角色状态")
    @RequestMapping(value = "/updateStatus/{id}", method = RequestMethod.POST)
    public CommonResult<?> updateStatus(@PathVariable Long id, @RequestParam(value = "status") Integer status) {

        UmsRole umsRole = new UmsRole();
        umsRole.setId(id);
        umsRole.setStatus(status);

        boolean update = roleService.updateById(umsRole);
        if (update) {
            return CommonResult.success();
        }
        return CommonResult.failed();
    }
}
